package com.yjz.ui.tab.top;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.yjz.ui.tab.common.ITabLayout;
import com.yjz.ui.tab.common.TabInfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 作用 :
 * 作者 : yjz
 * 时间 : 2024/10/16 10:00
 */
public class TabTopLayout extends HorizontalScrollView implements ITabLayout<TabTopView, TabInfo<?>> {

    private List<TabInfo<?>> infoList;
    private TabInfo<?> selectedInfo;
    private List<OnTabSelectedListener<TabInfo<?>>> tabSelectedChangeListeners = new ArrayList<>();

    public TabTopLayout(Context context) {
        this(context, null);
    }

    public TabTopLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabTopLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setVerticalScrollBarEnabled(false);
    }

    @Override
    public TabTopView findTab(@NonNull TabInfo<?> data) {
        ViewGroup viewGroup = getRootLayout(false);
        int childCount = viewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = viewGroup.getChildAt(i);
            if (view instanceof TabTopView) {
                TabTopView tabTopView = (TabTopView) view;
                if (data == tabTopView.getTabInfo()) {
                    return tabTopView;
                }
            }
        }
        return null;
    }

    @Override
    public void addTabSelectChangeListener(OnTabSelectedListener<TabInfo<?>> listener) {
        tabSelectedChangeListeners.add(listener);
    }

    @Override
    public void defaultSelected(@NonNull TabInfo<?> defaultInfo) {
        onSelect(defaultInfo);
    }

    @Override
    public void inflateInfo(@NonNull List<TabInfo<?>> infoList) {
        if (infoList.isEmpty()) return;
        this.infoList = infoList;
        LinearLayout targetView = getRootLayout(true);
        selectedInfo = null;

        //清除之前添加的HiTabTop listener，Tips：Java foreach remove问题
        Iterator<OnTabSelectedListener<TabInfo<?>>> iterator = tabSelectedChangeListeners.iterator();
        while (iterator.hasNext()) {
            if (iterator.next() instanceof TabInfo) {
                iterator.remove();
            }
        }
        for (TabInfo<?> tabInfo : infoList) {
            TabTopView tabTopView = new TabTopView(getContext());
            tabSelectedChangeListeners.add(tabTopView);
            tabTopView.setTabDataInfo(tabInfo);
            tabTopView.setOnClickListener(view -> onSelect(tabInfo));
            targetView.addView(tabTopView);
        }
    }

    /**
     * tab 选中
     */
    private void onSelect(TabInfo<?> info) {
        for (OnTabSelectedListener<TabInfo<?>> tabSelectedChangeListener : tabSelectedChangeListeners) {
            tabSelectedChangeListener.onTabSelectedChange(infoList.indexOf(info), selectedInfo, info);
        }
        this.selectedInfo = info;
    }

    private LinearLayout getRootLayout(boolean clear) {
        LinearLayout rootView = (LinearLayout) getChildAt(0);
        if (rootView == null) {
            rootView = new LinearLayout(getContext());
            rootView.setOrientation(LinearLayout.HORIZONTAL);
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            addView(rootView, layoutParams);
        } else if (clear) {
            rootView.removeAllViews();
        }
        return rootView;
    }
}
